﻿using AutoMapper;
using MediatR;
using S.RBAC.Api.Application.Command.StaffCommand;
using S.RBAC.Domain.Model;
using S.RBAC.Infrastructure.Interface;

namespace S.RBAC.Api.Application.CommandHandlers.StaffHandler
{
    public class StaffCreateCommandHandler : IRequestHandler<StaffCreateCommand, int>
    {
        protected readonly IStaffRepository _staff;
        protected readonly IMapper _mapper;

        public StaffCreateCommandHandler(IStaffRepository staff, IMapper mapper)
        {
            _staff = staff;
            _mapper = mapper;
        }

        public async Task<int> Handle(StaffCreateCommand request, CancellationToken cancellationToken)
        {
            //var staffInfo = _mapper.Map<Staff>(request);

            var info = await _staff.GetStaffList(t => t.EmployeeID == request.EmployeeID);

            var list = info.FirstOrDefault();

            if (list != null)
            {
                return 0;
            }

            return await _staff.AddStaff(new Staff
            {
                StaffName = request.StaffName,
                Gender = request.Gender,
                EmployeeID = request.EmployeeID,
                IDNumber = request.IDNumber,
                Phone = request.Phone,
                Photograph = request.Photograph,
                UnitName = request.UnitName,
                StaffStateId = request.StaffStateId,
                DepartmentId = request.DepartmentId,
                EntryDate = request.EntryDate,
                PostId = request.PostId,
                StationId = request.StationId,
                AccountNumber = request.AccountNumber,
                Password = request.Password,
                RoleId = request.RoleId,
                Area = request.Area,
                Location = request.Location,
                CreateBy = "孔令琪",
                CreateDate = DateTime.Now,
                UpdateBy = "",
                UpdateDate = null,
                IsDel = false
            });
        }
    }
}
